﻿using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Text;
using TTEditor;
using TTEditor.Resource;

namespace TTEngine.AssetExt
{
    class Texture2DConverter : TTEditor.Resource.IConverter
    {
        public string extname => ".jpg|.jpeg|.png";
        public bool NeedConvert(byte[] srcdata, AssetMetaData data)
        {
            bool bconvert = false;
            if (data.head == null)
            {
                data.head = new AssetHead();
                data.head.MagicHead = "TTA";
                data.head.version = 1;
                data.head.AssetDataInfo = new AssetDataInfo[0];
                data.head.srcfileInfo = null;
                bconvert = true;
            }

            if (!bconvert)
            {
                var filedata = AssetDataTool.ComputeDataInfo(srcdata);
                if (filedata.EqualWith(data.head.srcfileInfo) == false)
                {
                    data.head.srcfileInfo = filedata;
                    bconvert = true;
                }
            }
            return bconvert;
        }
        public void Convert(byte[] srcdata, AssetMetaData data)
        {
            data.head.AssetType = 0xff01;
            data.head.srcfileInfo = AssetDataTool.ComputeDataInfo(srcdata);
            data.assetDatas = new Dictionary<string, byte[]>();
            byte[] outdata = null;
            using (var ms = new System.IO.MemoryStream())
            {
                ms.WriteByte(1);
                byte[] buf = BitConverter.GetBytes((UInt32)srcdata.Length);
                ms.Write(buf, 0, 4);
                ms.Write(srcdata);
                outdata = ms.ToArray();
            }

            data.AddPlatformData(AssetPlatform.Windows, outdata);
        }


        //这个资源转换器是否有GUI扩展
        public bool HaveGUIExt => true;

        public void OnGUI()
        {
            ImGui.Text("这是扩展的ConvertUI,资源的Preview功能不应该做在这里，要做也可以");
            TTEngine.Editor.ReflectionGUI.OnGUI(this);
        }

        public string hi;
        public int abc;
    }
}
